package autoteam.ui;

import alarm.AlarmSystem;
import autoschedule.NoticeDialog;
import autoschedule.ui.*;
import autoschedule.*;

import autoteam.Local;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Calendar;
import ui.BackgroundPanel;

import controller.RSMSController;

/**
 * Select your sport!
 * @author Erick
 */

public class SportsSelectionUI extends JFrame
{
  private static final long serialVersionUID = 3568778665671772288L;
  AutoTeamDeskTop2 theFrame;
  AutoScheduleDeskTop sched;
  private BackgroundPanel BGpanel;
  private JButton baseball_button;
  private JButton basketball_button;
  private JButton football_button;
  private JFrame InitialFrame;
  private JLabel label1;
  private JButton soccer_button;
  
  private final int frameWidth = 640;
  private final int frameHeight = 480;
  private final Dimension frameSize = new Dimension(frameWidth, frameHeight);
  
  private Toolkit sizer = Toolkit.getDefaultToolkit(); 
  private Dimension screenSize = sizer.getScreenSize();
  
  private final int screenCenterX = (screenSize.width-frameWidth)/2;
  private final int screenCenterY = (screenSize.height-frameHeight)/2;
  
  // End of variables declaration//GEN-END:variables
  /*
   * RSMSController initialization
   */
  private RSMSController controller;

  /** Creates new form initial_screen */
  public SportsSelectionUI(RSMSController newController)
  {
    controller = newController;

    initComponents();

    this.setSize( frameSize );
    this.setLocation( screenCenterX, screenCenterY );
    this.setResizable(false);
  }

  private void initComponents()
  {

    InitialFrame = new JFrame();
    InitialFrame.setLayout(null);
    BGpanel = new BackgroundPanel("./trunk/img/Background.png");


    this.setContentPane(BGpanel);
    baseball_button = new JButton();
    baseball_button.setFocusPainted(false);
    basketball_button = new JButton();
    basketball_button.setFocusPainted(false);
    football_button = new JButton();
    football_button.setFocusPainted(false);
    soccer_button = new JButton();
    soccer_button.setFocusPainted(false);
    label1 = new JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Select Your Sport");
    setResizable(true);

    baseball_button.setIcon(new javax.swing.ImageIcon("./trunk/img/Baseball_48.png")); // NOI18N
    baseball_button.setText("Baseball");
    baseball_button.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        baseball_buttonActionPerformed(evt);
      }
    });

    basketball_button.setIcon(new javax.swing.ImageIcon("./trunk/img/Basketball_48.png")); // NOI18N
    basketball_button.setText("Basketball");
    basketball_button.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        basketball_buttonActionPerformed(evt);
      }
    });

    football_button.setIcon(new javax.swing.ImageIcon(
        "./trunk/img/Football_48.png")); // NOI18N
    football_button.setText("Football");
    football_button.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        football_buttonActionPerformed(evt);
      }
    });

    soccer_button
        .setIcon(new javax.swing.ImageIcon("./trunk/img/Soccer_48.png")); // NOI18N
    soccer_button.setText("Soccer");
    soccer_button.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        soccer_buttonActionPerformed(evt);
      }
    });

    label1.setHorizontalAlignment(JLabel.CENTER);
    label1.setVerticalAlignment(JLabel.NORTH);
    label1.setFont(new java.awt.Font("Arial", 1, 20));
    label1.setText("Select your sport...");
    label1.setForeground(Color.white);
    
    
    this.setLayout(new GridBagLayout());
    GridBagConstraints constrain = new GridBagConstraints();

    constrain.fill = GridBagConstraints.HORIZONTAL;
    constrain.insets = new Insets(20, 20, 20, 20); // top padding

    constrain.weightx = 1;
    constrain.gridx = 1;
    constrain.gridy = 0;
    constrain.gridwidth = 2;
    this.add(label1, constrain);

    constrain.weightx = 1;
    constrain.gridx = 0;
    constrain.gridy = 1;
    this.add(baseball_button, constrain);

    constrain.weightx = 1;
    constrain.gridx = 2;
    constrain.gridy = 1;
    this.add(basketball_button, constrain);

    constrain.weightx = 1;
    constrain.gridx = 0;
    constrain.gridy = 3;
    this.add(football_button, constrain);

    constrain.weightx = 1;
    constrain.gridx = 2;
    constrain.gridy = 3;
    constrain.insets = new Insets(0, 20, 20, 20); // top padding
    constrain.anchor = GridBagConstraints.PAGE_END; // bottom of space
    this.add(soccer_button, constrain);

    ImageIcon ImageIcon = new ImageIcon("./trunk/img/Compass.png");
    Image Image = ImageIcon.getImage();
    this.setIconImage(Image);

    pack();
  }

  private void intialize(String currentSport)
  {
//    try
//    {
//      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//    }
//    catch (Exception e)
//    {}
//
//    UIManager.LookAndFeelInfo[] inf = UIManager.getInstalledLookAndFeels();
//    for (int i = 0; i < inf.length; i++)
//      System.out.println(inf[i].getName());

    // Set up the localization
    String language;
    String country;
    language = new String("en");
    country = new String("US");
    Local.initialize(language, country);
    
    controller.setCurSport(currentSport);	  
    sched = new AutoScheduleDeskTop(controller);
    theFrame = new AutoTeamDeskTop2(controller);

  }

  private void startRSMS(){
	  controller.setInactiveFrame(sched);
	  controller.replaceActiveFrame(theFrame);
	  
      //Create A new Alarm System for the Current User
      AlarmSystem notices = new AlarmSystem(controller.getCurUser().getUsername()+"_"+controller.getCurSport());
      //System.out.println("The Hours will be of lenght " + (24-Calendar.getInstance().getTime().getHours()));
      // System.out.println("The Username is : "+ controller.getCurUser().getUsername());
      NoticeDialog r = new NoticeDialog(this,notices.pendingNoticeUntilTime(24-Calendar.getInstance().getTime().getHours()),controller.getCurUser().getUsername());

  }
  
   private void basketball_buttonActionPerformed(ActionEvent evt)
  {
    // GEN-FIRST:event_basketball_buttonActionPerformed
    this.intialize("Basketball");
    this.actionHelper("./trunk/img/basketball_logo.png","./trunk/img/Basketball_Ball.png", 3);
    startRSMS();
  }

  private void football_buttonActionPerformed(ActionEvent evt)
  {
    // GEN-FIRST:event_football_buttonActionPerformed
    this.intialize("Football");
    this.actionHelper("./trunk/img/football_logo.png","./trunk/img/Football_Ball.png", 4);
    startRSMS();
  }

  private void soccer_buttonActionPerformed(ActionEvent evt)
  {
    // GEN-FIRST:event_soccer_buttonActionPerformed
    this.intialize("Soccer");
    this.actionHelper("./trunk/img/soccer_logo.png","./trunk/img/Soccer_Ball.png", 1);
    startRSMS();
  }

  private void baseball_buttonActionPerformed(ActionEvent evt)
  {
    // GEN-FIRST:event_baseball_buttonActionPerformed
    this.intialize("Baseball");
    this.actionHelper("./trunk/img/baseball_logo.png","./trunk/img/Baseball_Ball.png", 2);
    startRSMS();
  }

  private void actionHelper(String logo, String icon, int type)
  {
    theFrame.change_logo(logo, type);
    ImageIcon ImageIcon = new ImageIcon(icon);
    Image Image = ImageIcon.getImage();
    theFrame.setIconImage(Image);
    sched.setIconImage(Image);
    //startRSMS();
  }

  public static void main(String args[])
  {
    RSMSController controller = new RSMSController();
    SportsSelectionUI selectionUI = new SportsSelectionUI( controller );
    selectionUI.setVisible(true);
  }

}
